package com.cimc.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Assert;
/**
 * http请求
 * @author zting
 *
 */
public class HttpUtils {

     /** 
      * 执行一个HTTP GET请求，返回请求响应的HTML 
      * 
      * @param url                 请求的URL地址 
      * @param queryString 请求的查询参数,可以为null 
      * @return 返回请求响应的HTML 
      */ 
     public static String doGet(String url, String queryString) { 
             String response = null; 
             HttpClient client =  new DefaultHttpClient();
             HttpGet get = new HttpGet(url+ queryString);  
             HttpResponse res;
			try {
				res = client.execute(get);
				 if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
	                 InputStream is = res.getEntity().getContent();  
	                 response = inStream2String(is);  
	                 Assert.assertEquals(response, "GET_SUCCESS");  
	             } 
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
            
//             HttpMethod method = new GetMethod(url); 
//             try { 
//                     if (StringUtils.isNotBlank(queryString)) 
//                             method.setQueryString(URIUtil.encodeQuery(queryString)); 
//                     client.executeMethod(method); 
//                     if (method.getStatusCode() == HttpStatus.SC_OK) { 
//                             response = method.getResponseBodyAsString(); 
//                     } 
//             } catch (URIException e) { 
//                     log.error("执行HTTP Get请求时，编码查询字符串“" + queryString + "”发生异常！", e); 
//             } catch (IOException e) { 
//                     log.error("执行HTTP Get请求" + url + "时，发生异常！", e); 
//             } finally { 
//                     method.releaseConnection(); 
//             } 
             return response; 
     } 

     /** 
      * 执行一个HTTP POST请求，返回请求响应的HTML 
      * 
      * @param url        请求的URL地址 
      * @param params 请求的查询参数,可以为null 
      * @return 返回请求响应的HTML 
      */ 
     public static String doPost(String url, Map<String, String> params) { 
             String response = null; 
             HttpClient client = new DefaultHttpClient(); 
//             HttpMethod method = new PostMethod(url); 
//             for (Iterator it = params.entrySet().iterator(); it.hasNext();) { 
//
//             } 
//             //设置Http Post数据 
//             if (params != null) { 
//                     HttpMethodParams p = new HttpMethodParams(); 
//                     for (Map.Entry<String, String> entry : params.entrySet()) { 
//                             p.setParameter(entry.getKey(), entry.getValue()); 
//                     } 
//                     method.setParams(p); 
//             } 
//             try { 
//                     client.executeMethod(method); 
//                     if (method.getStatusCode() == HttpStatus.SC_OK) { 
//                             response = method.getResponseBodyAsString(); 
//                     } 
//             } catch (IOException e) { 
//                     log.error("执行HTTP Post请求" + url + "时，发生异常！", e); 
//             } finally { 
//                     method.releaseConnection(); 
//             } 

             return response; 
     } 
     
     // 将输入流转换成字符串  
     private static String inStream2String(InputStream is) throws Exception {  
         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
         byte[] buf = new byte[1024];  
         int len = -1;  
         while ((len = is.read(buf)) != -1) {  
             baos.write(buf, 0, len);  
         }  
         return new String(baos.toByteArray());  
     } 
}
